import createAPI from './fetch';
import Consts from './const';


function createUrl(apiCode) {
  return `${HOST}/${apiCode}`;
}

function createOptions(params) {
  return {
    mode: 'cors',
    credentials: 'include',
    body: {
      ...params,
    }
  };
}
function createJsonOptions(params) {
  return {
    mode: 'cors',
    credentials: 'include',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(params)
  };
}
// 获取资源
export function createGetAPI(apiCode, params?) {
  return createAPI(Consts.HttpMethods.GET, createUrl(apiCode), createOptions(params));
}
// 删除资源
export function createDeleteAPI(apiCode, params?) {
  return createAPI(Consts.HttpMethods.DELETE, createUrl(apiCode), createOptions(params));
}
// 新建资源或更新资源
export function createPostAPI(apiCode, params?) {
  return createAPI(Consts.HttpMethods.POST, createUrl(apiCode), createOptions(params));
}
export function createPostJsonAPI(apiCode, params?) {
  return createAPI(Consts.HttpMethods.POST, createUrl(apiCode), createJsonOptions(params));
}
// 更新资源
export function createPutAPI(apiCode, params?) {
  return createAPI(Consts.HttpMethods.PUT, createUrl(apiCode), createOptions(params));
}
export function createPutJsonAPI(apiCode, params?) {
  return createAPI(Consts.HttpMethods.PUT, createUrl(apiCode), createJsonOptions(params));
}
// 局部更新资源
export function createPatchAPI(apiCode, params?) {
  return createAPI(Consts.HttpMethods.PATCH, createUrl(apiCode), createOptions(params));
}
export function createPatchJsonAPI(apiCode, params?) {
  return createAPI(Consts.HttpMethods.PATCH, createUrl(apiCode), createJsonOptions(params));
}
